#!/bin/bash
cur_path="$(pwd)"
execs() {
    local name=$(docker ps | grep "$1" | grep "wfs" | awk '{print $1}')
    if [ -z "$name" ]; then
        echo -e "${Error} ${RedBG} 没有找到 $1 容器! ${Font}"
        exit 1
    fi
    docker exec -it "$name" /bin/sh -c "$2"
}
check_docker() {
    docker --version &> /dev/null
    if [ $? -ne  0 ]; then
        echo -e "${Error} ${RedBG} 未安装 Docker！${Font}"
        exit 1
    fi
    docker-compose version &> /dev/null
    if [ $? -ne  0 ]; then
        docker compose version &> /dev/null
        if [ $? -ne  0 ]; then
            echo -e "${Error} ${RedBG} 未安装 Docker-compose！${Font}"
            exit 1
        fi
    fi
    if [[ -n `docker compose version | grep -E "\sv*1"` ]]; then
        $COMPOSE version
        echo -e "${Error} ${RedBG} Docker-compose 版本过低，请升级至v2+！${Font}"
        exit 1
    fi
}
env_get() {
    local key=$1
    local value=`cat ${cur_path}/.env | grep "^$key = " | awk -F ' = ' '{print $2}'`
    echo "$value"
}

####################################################################################
####################################################################################
####################################################################################

shift 1
check_docker
# 初始化文件
rm -rf composer.lock
mkdir -p "${cur_path}/docker/log/supervisor"
mkdir -p "${cur_path}/docker/mysql/data"
chmod -R 775 "${cur_path}/docker/log/supervisor"
chmod -R 775 "${cur_path}/docker/mysql/data"
chmod -R 775 "${cur_path}/docker/crontab/crontab.sh"
# 启动容器 
docker compose up -d
# 启动定时器
# exec php "crontab /var/spool/cron/crontabs/cron"
# exec php "cron restart >> /dev/null 2>&1"
# 安装 composer依赖
execs php "composer install"
# 安装数据库
execs php "php think migrate:run"
execs php "php think seed:run"
# 执行软链接
# execs php "php think link"
# 安装完成
echo "\033[32m 安装完成  \033[0m"
echo "\033[32m 地址： http://127.0.0.1:$(env_get APP_PORT) \033[0m"
echo "\033[32m 账号： admin \033[0m"
echo "\033[32m 密码： 1 \033[0m"

